interface myFetchOptions {
  headers?: Record<string, string>;
  [key: string]: any;
}

export const useHttpFetch = (url: string, opt?: myFetchOptions) => {
  const token = useCookie("token");
  const headers = {
    ...opt?.headers,
    ...(token ? { Authorization: `Bearer ${token}` } : {}),
  };

  if (opt?.headers) {
    opt.headers = headers;
  }

  return useFetch(url, {
    ...opt,
    baseURL: "",
    onRequest({ request, options }) {
      console.log("request", request);
    },
    onRequestError({ request, options, error }) {
      console.log("request", request);
    },
    onResponse({ request, response, options }) {
      // return response._data;
      console.log("response", response);
    },
    onResponseError({ request, options, error }) {},
  });
};

// 定义接口
export const useInfoFetch = (opt?: myFetchOptions) => {
  return useHttpFetch("/user/info", opt);
};
